package com.iot.service;

import com.iot.mapper.DTdevice_statusMapper;
import com.iot.mapper.Tdevice_statusMapper;
import com.iot.pojo.MTdevice_status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class Tdevice_statusServiceImpl implements Tdevice_statusService {
    @Autowired
    private Tdevice_statusMapper mapper;
    @Autowired
    private DTdevice_statusMapper dmapper;
    @Override
    public int getRecordCount(String sWhere) {
        return mapper.getRecordCount(sWhere);
    }
    @Override
    public int insert(MTdevice_status mTdevice_status) throws Exception {
        int count = 0;
        count = getRecordCount(" AND SNAME='" + mTdevice_status.getSNAME() + "'");
        if (count > 0)
            throw new Exception("该名称已存在");
        count = getRecordCount(" AND CODE='" + mTdevice_status.getCODE() + "'");
        if (count > 0)
            throw new Exception("该状态码已存在");
        return mapper.insert(mTdevice_status);
    }

    @Override
    public void update(MTdevice_status mTdevice_status) throws Exception {
        int count = 0;
        count = getRecordCount(" AND SNAME='" + mTdevice_status.getSNAME() + "' AND STATUS_ID!="+mTdevice_status.getSTATUS_ID()+"");
        if (count > 0)
            throw new Exception("该名称已存在");
        count = getRecordCount(" AND CODE='" + mTdevice_status.getCODE() + "' AND STATUS_ID!="+mTdevice_status.getSTATUS_ID()+"");
        if (count > 0)
            throw new Exception("该状态码已存在");
        mapper.update(mTdevice_status);
    }

    @Override
    public void deleteById(int id) {
        mapper.deleteById(id);
    }

    @Override
    public MTdevice_status single(Integer id) {
        return mapper.single(id);
    }

    @Override
    public List<MTdevice_status> fetch(String sWhere) {
        return mapper.fetch(sWhere);
    }

    @Override
    public List<MTdevice_status> fetchByPage(Map<String, Object> map) {

        return dmapper.fetchByPage(map);
    }
}
